//
//  main.swift
//  75-颜色分类
//
//  Created by lhc on 2023/10/6.
//

import Foundation

class Solution {
    
    static func sortColors(_ nums: inout [Int]) {
        var i = 0
        var lessThan = 0
        var greatThan = nums.count
        
        while i < greatThan {
            if nums[i] == 0 {
                exchange(nums: &nums, i: i, j: lessThan)
                lessThan += 1
                i += 1
            }else if nums[i] == 1 {
                i += 1
            }else if nums[i] == 2 {
                greatThan -= 1
                exchange(nums: &nums, i: i, j: greatThan)
            }
        }
        
    }
    
    static private func exchange(nums: inout [Int], i: Int, j: Int) {
        let temp = nums[i]
        nums[i] = nums[j]
        nums[j] = temp
    }
}

var nums1 = [2, 0, 1, 2, 1, 0]
var nums2 = [2, 0, 1]

Solution.sortColors(&nums1)
Solution.sortColors(&nums2)

print(nums1)
print(nums2)

